﻿namespace ServerApp
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Server;

    public class PlaceService
    {

        /// <summary>
        /// Get All Places
        /// </summary>
        /// <param name="language">Current language</param>
        /// <returns>List if places</returns>
        public static List<PlaceView> GetPlaces(string language)
        {
            try
            {
                List<PlaceView> placeViewList = null;
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                IQueryable<Place> placeList = (from p in entity.Places select p);
                if (placeList != null && placeList.Count() > 0)
                {
                    placeViewList = new List<PlaceView>();
                    foreach (Place item in placeList)
                    {
                        PlaceView placeView = new PlaceView(item.PlaceId)
                        {
                            Name = item.Name
                        };
                        IQueryable<PlaceLocalization> placeLocalizationList = (from p in entity.PlaceLocalizations
                                                                               where p.FK_PlaceId.Equals(item.PlaceId)
                                                                               select p);
                        foreach (PlaceLocalization itemL in placeLocalizationList)
                        {
                            if (itemL.Language.Equals(language))
                            {
                                placeView.Address = itemL.Address;
                            }
                        }

                        placeViewList.Add(placeView);
                    }
                }
                entity.SaveChanges();
                return placeViewList;
            }
            catch
            {
                return null;
            }

        }

        /// <summary>
        /// Get place
        /// </summary>
        /// <param name="placeAll">Full place information with list of localizations</param>
        /// <returns>Full place information with list of localizations</returns>
        public static PlaceAll GetPlaceAll(int placeAllId)
        {
            try
            {
                PlaceAll placeAll = null;
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                IQueryable<Place> placeList = (from p in entity.Places where p.PlaceId == placeAllId select p);
                if (placeList != null && placeList.Count() > 0)
                {
                    Place place = placeList.First();
                    if (place.PlaceId > 0)
                    {
                        placeAll = new PlaceAll(place.PlaceId)
                        {
                            Name = place.Name
                        };
                        IQueryable<PlaceLocalization> placeLocalizationList = (from p in entity.PlaceLocalizations
                                                                               where p.FK_PlaceId.Equals(placeAllId)
                                                                               select p);
                        foreach (PlaceLocalization item in placeLocalizationList)
                        {
                            if (placeAll.Localizations == null)
                            {
                                placeAll.Localizations = new List<PlaceLocalizationModel>();
                            }
                            placeAll.Localizations.Add(new PlaceLocalizationModel()
                            {
                                Address = item.Address,
                                Language = item.Language
                            });
                        }
                    }
                }
                if (placeAll != null)
                {
                    entity.SaveChanges();
                    return placeAll;
                }
                else
                {
                    return PlaceAll.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        public static PlaceAll GetPlaceAll(string placeName)
        {
            try
            {
                PlaceAll placeAll = null;
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                IQueryable<Place> placeList = (from p in entity.Places where p.Name.Equals(placeName) select p);
                if (placeList != null && placeList.Count() > 0)
                {
                    Place place = placeList.First();
                    if (place.PlaceId > 0)
                    {
                        placeAll = new PlaceAll(place.PlaceId)
                        {
                            Name = place.Name
                        };
                        IQueryable<PlaceLocalization> placeLocalizationList = (from p in entity.PlaceLocalizations
                                                                               where p.FK_PlaceId.Equals(place.PlaceId)
                                                                               select p);
                        foreach (PlaceLocalization item in placeLocalizationList)
                        {
                            if (placeAll.Localizations == null)
                            {
                                placeAll.Localizations = new List<PlaceLocalizationModel>();
                            }
                            placeAll.Localizations.Add(new PlaceLocalizationModel()
                            {
                                Address = item.Address,
                                Language = item.Language
                            });
                        }
                    }
                }
                if (placeAll != null)
                {
                    entity.SaveChanges();
                    return placeAll;
                }
                else
                {
                    return PlaceAll.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Add place in base
        /// </summary>
        /// <param name="placeAll">Full place information with list of localizations</param>
        /// <returns>Full place information with list of localizations</returns>
        public static PlaceAll AddPlace(PlaceAll placeAll)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                Place place = new Place()
                {
                    Name = placeAll.Name
                };
                entity.AddToPlaces(place);
                entity.SaveChanges();
                if (place.PlaceId > 0)
                {
                    placeAll.Id = place.PlaceId;
                    foreach (PlaceLocalizationModel item in placeAll.Localizations)
                    {
                        entity.AddToPlaceLocalizations(new PlaceLocalization()
                        {
                            FK_PlaceId = place.PlaceId,
                            Language = item.Language,
                            Address = item.Address
                        });
                    }
                }
                if (placeAll.Id > 0)
                {
                    entity.SaveChanges();
                    return placeAll;
                }
                else
                {
                    return PlaceAll.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Update place
        /// </summary>
        /// <param name="placeAll">Full place information with list of localizations</param>
        /// <returns>Full place information with list of localizations</returns>
        public static PlaceAll UpdatePlace(PlaceAll placeAll)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                Place place = null;
                IQueryable<Place> placeList = (from p in entity.Places where p.PlaceId == placeAll.Id select p);
                if (placeList != null && placeList.Count() > 0)
                {
                    place = placeList.First();
                    if (place.PlaceId > 0)
                    {
                        place.Name = placeAll.Name;

                        foreach (PlaceLocalization item in entity.PlaceLocalizations)
                        {

                            if (item.FK_PlaceId.Equals(place.PlaceId))
                                entity.DeleteObject(item);
                        }

                        foreach (PlaceLocalizationModel item in placeAll.Localizations)
                        {
                            entity.AddToPlaceLocalizations(new PlaceLocalization()
                            {
                                FK_PlaceId = place.PlaceId,
                                Language = item.Language,
                                Address = item.Address
                            });
                        }
                    }
                }
                if (place != null)
                {
                    entity.SaveChanges();
                    return placeAll;
                }
                else
                {
                    return PlaceAll.Null;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Delete place from base
        /// </summary>
        /// <param name="placeId">Place identifaier</param>
        /// <returns>If operation is successful return true,elese - falentity</returns>
        public static bool DeletePlace(int placeId)
        {
            try
            {
                InternetShopBaseEntities entity = ServerApp.SingleEntity.GetInstance();
                Place place = (from p in entity.Places where p.PlaceId == placeId select p).First();
                if (place.PlaceId > 0)
                {
                    foreach (PlaceLocalization item in entity.PlaceLocalizations)
                    {

                        if (item.FK_PlaceId.Equals(placeId))
                            entity.DeleteObject(item);
                    }
                }
                entity.DeleteObject(place);
                entity.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
